package com.serve.oa.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serve.oa.dto.LeaveDto;
import com.serve.oa.dto.LeaveFlowDto;
import com.serve.oa.dto.TaskDto;
import com.serve.oa.entity.*;
import com.serve.oa.mapper.TblFlowNodeMapper;
import com.serve.oa.mapper.TblTaskMapper;
import com.serve.oa.service.TblTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author __tyw
 * @since 2022-12-12
 */
@Service
public class TblTaskServiceImpl extends ServiceImpl<TblTaskMapper, TblTask> implements TblTaskService {


    @Autowired
    TblTaskMapper tblTaskMapper;

    @Autowired
    TblFlowNodeMapper tblFlowNodeMapper;



    @Override
    public int add(Long id,int userid) {

        TblTask tblTask = new TblTask();
        tblTask.setFlowNo(Math.toIntExact(id));
        tblTask.setCurNode(1);
        tblTask.setUserId(userid);
        tblTask.setCreated(LocalDateTime.now());

        tblTaskMapper.addyw(tblTask);
        return  tblTask.getTaskId();
    }

    @Override
    @DS("db2")
    public List<Map> listAll() {
        return tblTaskMapper.listAll();
    }
    @Override
    public int addLeave(LeaveDto leaveDto) {
        return tblTaskMapper.addLeave(leaveDto);
    }


    @Override
    public int addLeaveFlow(LeaveFlowDto leaveDto) {
        return tblTaskMapper.addLeaveFlow(leaveDto);
    }

    @Override
    public void inLog(TblLog tblLog) {
        tblTaskMapper.inlog(tblLog);
    }

    @Override
    @Async
    public void ybdy() throws InterruptedException {
        // 异步调用

        TimeUnit.SECONDS.sleep(6);
        System.out.println(3212313);
    }

    @Override
    public Page<TaskDto> getMytaskPage(Page iPage, Long id) {

        return tblTaskMapper.getMytaskPage(iPage,id);

    }

    @Override

    public void delmylist(String id) {

        tblTaskMapper.delTask(id);
        tblTaskMapper.delLeave(id);
        tblTaskMapper.delBxd(id);
        tblTaskMapper.dellog(id);

    }

    @Override
    public TtbLeave getLeave(Integer taskId) {

        return tblTaskMapper.getLeave(taskId);
    }

    @Override

   // @Cacheable(value = {"menuById"}, key = "'sadadaaaa'")
    public List<TblLog> getLog(Integer taskId) {
        return tblTaskMapper.getLog(taskId);
    }


    @Override
    public Map getNolist(){
        List<TblFlowNode> nolist = tblFlowNodeMapper.selectList(new QueryWrapper<>());



        Map map = new HashMap<>();
        nolist.forEach(i->{
            if(map.containsKey(i.getFlowNo())){

                List ss1 = (List) map.get(i.getFlowNo());
                ss1.add(i);
                map.put(i.getFlowNo(),ss1);


            }else {
                ArrayList ss = new ArrayList<>();
                ss.add(i);
                map.put(i.getFlowNo(),ss);
            }

        });
        return map;
    }
    @Override
    public List<TaskDto> myHandleTask(Long id) {
        //赛选出未完成的task
        List<TaskDto> task = tblTaskMapper.getNfnList();



        Map map = this.getNolist();


        List<TaskDto> newTask = new ArrayList<TaskDto>();

        task.forEach(i->{
            List<TblFlowNode> ds = (List) map.get(i.getFlowNo());

            TblFlowNode sd = new TblFlowNode();

            ds.forEach(item->{
                if(item.getCurNode() == (i.getCurNode()+1)){
                    sd.setType(item.getType());
                    sd.setFlowNo(item.getFlowNo());
                    sd.setOrganId(item.getOrganId());
                    sd.setFlowNodeName(item.getFlowNodeName());
                    sd.setCurNode(item.getCurNode());
                }
            });


            //System.out.println("21313"+sd);


            if(sd.getType() != null){
                if(sd.getType() == 1){
                    List<ComUserOrganization> ad = tblTaskMapper.getOrg(i.getUserId());
                    ad.forEach(adi->{
                        if(adi.getUserId() == id){
                            newTask.add(i);
                        }
                    });

                }

                if(sd.getType() == 2){

                    List<Map> ad = tblTaskMapper.getMan(id,sd.getOrganId());


                    if(!ad.isEmpty()){
                        newTask.add(i);
                    }

                }

            }




        });

        return newTask;
       // return tblTaskMapper.myHandleTask(id);
    }

    @Override
    public Boolean nextNode(Integer id, Integer curNode, Integer flowNo) {
        Map map = this.getNolist();

        List<TblFlowNode> ds = (List) map.get(flowNo);

        List<TblFlowNode> sd =  ds.stream().filter(i->(curNode+1)==i.getCurNode()).collect(Collectors.toList());


        if(sd.get(0)!= null){
            TblTask task = getOne(new QueryWrapper<TblTask>().eq("task_id",id));
            task.setCurNode(sd.get(0).getCurNode());
            this.updateById(task);
            return true;
        }else {
            return false;
        }


    }

    @Override
    @Cacheable(value = {"time"}, key = "'shijian'")
    public String getTime(String id) {
        return LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
    }

    @Override
    public Map getFlowDeatil(Map<String, Object> variable) {

        Integer TableType = (Integer) variable.get("TableType");
        Integer formId = (Integer) variable.get("formId");
        if(TableType==1){

            new String();

            Map map = tblTaskMapper.getqjb(formId);
            map.put("type",5);
           return map;



        }else {
            return new HashMap();


        }

    }



    @Override
    public int addBxt(BxtLeave bxtDto) {
        return tblTaskMapper.addbxt(bxtDto);
    }

    @Override
    public BxtLeave getBxt(Integer taskId) {
        return tblTaskMapper.getBxt(taskId);
    }




}
